package com.example.library.controller;

import com.example.library.entity.Announcement;
import com.example.library.entity.Reservation;
import com.example.library.entity.Seat;
import com.example.library.entity.User;
import com.example.library.service.ReservationService;
import com.example.library.service.SeatService;
import org.antlr.v4.runtime.tree.pattern.ParseTreeMatch;
import org.antlr.v4.runtime.tree.pattern.ParseTreePattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.example.library.service.AnnouncementService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ReservationController {

    private final AnnouncementService announcementService;

    @Autowired
    public ReservationController(AnnouncementService announcementService) {
        this.announcementService = announcementService;
    }

    @GetMapping("/announcements")
    public String announcements(Model model) {
        List<Announcement> announcements = announcementService.findAll();
        model.addAttribute("announcements", announcements);
        return "announcements";
    }


    @Autowired
    private SeatService seatService;

    @Autowired
    private ReservationService reservationService;


    @GetMapping("/history")
    public String getHistory(Model model) {
        // 获取当前登录用户的所有预约记录
        List<Reservation> reservations = reservationService.getAllReservationsByUser(getCurrentUser());
        model.addAttribute("reservations", reservations);
        return "history";
    }

    @GetMapping("/reservations/cancel")
    public String cancelReservation(@RequestParam int id) {
        // 取消预约逻辑
        reservationService.cancelReservation(id);
        return "redirect:/history";
    }

    @PostMapping("/reservations/create")
    public String createReservation(@RequestParam int seatId,
                                    @RequestParam String startTime,
                                    @RequestParam String endTime,
                                    Model model) {
        try {
            Seat seat = seatService.getSeatById(seatId);
            if (seat == null || seat.getStatus() != 0) {
                return "redirect:/seats?error=seat_unavailable";
            }

            LocalDateTime start = LocalDateTime.parse(startTime.replace(' ', 'T'));
            LocalDateTime end = LocalDateTime.parse(endTime.replace(' ', 'T'));

            if (start.isAfter(end)) {
                return "redirect:/seats?error=invalid_time";
            }

            Reservation reservation = new Reservation();
            reservation.setSeat(seat);
            reservation.setStartTime(start);
            reservation.setEndTime(end);
            User currentUser = getCurrentUser(); // 实现获取当前用户的方法
            reservation.setUser(currentUser);

            reservationService.createReservation(reservation);
            seat.setStatus(1); // 更新座位状态为已预约
            seatService.updateSeat(seat);

            return "redirect:/history?success=true"; // 预约成功后重定向并提示成功
        } catch (Exception e) {
            return "redirect:/seats?error=" + e.getMessage(); // 返回错误信息
        }
    }

    // 模拟获取当前用户的方法，实际应用中应替换为真实实现
    private User getCurrentUser() {
        // 实现获取当前登录用户逻辑
        return new User(1, "testUser"); // 示例用户
    }
}